package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entitry.Category;
import com.itheima.reggie.entitry.Dish;
import com.itheima.reggie.entitry.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Override
    public boolean delete(Long id) {

        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<>();
        lqwDish.eq(Dish::getCategoryId,id);
        int countDish = dishService.count(lqwDish);
        if (countDish > 0){
            throw new CustomException("该菜系里尚有菜品，无法删除！");
        }

        LambdaQueryWrapper<Setmeal> lqwSetmeal = new LambdaQueryWrapper<>();
        lqwSetmeal.eq(Setmeal::getCategoryId,id);
        int countSetmeal = setmealService.count(lqwSetmeal);
        if (countSetmeal > 0 ){
            throw new CustomException("该菜系里尚有套餐，无法删除！");
        }

        return super.removeById(id);
    }
}
